﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class GovOfficeRegionDataAccess : IGovOfficeRegionDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static GovOfficeRegionDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="GovOfficeRegionDataAccess"/> class.
        /// </summary>
        static GovOfficeRegionDataAccess() { Instance = new GovOfficeRegionDataAccess(); }


        #region Implementation of IGovOfficeRegionDataAccess

                /// <summary>
                /// Extracts the raw data to Entity Government office region
                /// </summary>
                /// <param name="dataReader">The data reader.</param>
                /// <returns></returns>
                public GovOfficeRegion ExtractRawData(IDataRecord dataReader)
                {
                    if(dataReader!=null)
                    {
                        var govOfficeRegion = new GovOfficeRegion
                                                  {
                                                      GovOfficeRegionId = (Guid) dataReader["GovOfficeRegionId"],
                                                      GovOfficeRegionName = dataReader["GovOfficeRegionName"].ToString(),
                                                      Description = dataReader["Description"].ToString()
                                                  };
                        govOfficeRegion.SubDescription = govOfficeRegion.Description.Length > 15 ? govOfficeRegion.Description.Substring(0, 15) : govOfficeRegion.Description;
                    govOfficeRegion.IsActive = (bool) dataReader["IsActive"];
                    return govOfficeRegion;
                    }
                    return null;
                }

                /// <summary>
                /// Gets the list of government office region.
                /// </summary>
                /// <param name="includeInactive">if set to <c>true</c> [include in active].</param>
                /// <returns></returns>
                public Collection<GovOfficeRegion> GetGovOfficeRegion(bool includeInactive)
                {
                    new Collection<GovOfficeRegion>();
                    if(includeInactive)
                    {
                        return GetAll();
                    }
                    return GetAllActive();
                }

                /// <summary>
                /// Gets all Government office region, active and in-active
                /// </summary>
                /// <returns></returns>
                public Collection<GovOfficeRegion> GetAll()
                {
                    var listGovOfficeRegion = new Collection<GovOfficeRegion>();
                    using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetAllGovOfficeRegion))
                    {
                        while (reader.Read())
                        {
                            var result = ExtractRawData(reader);
                            listGovOfficeRegion.Add(result);
                        }
                        return listGovOfficeRegion;
                    }
                }

                /// <summary>
                /// Gets all ACTIVE government office region 
                /// </summary>
                /// <returns></returns>
                public Collection<GovOfficeRegion> GetAllActive()
                {
                    var listGovOfficeRegion = new Collection<GovOfficeRegion>();
                    using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetAllActiveGovOfficeRegion))
                    {
                        while (reader.Read())
                        {
                            var result = ExtractRawData(reader);
                            listGovOfficeRegion.Add(result);
                        }
                        return listGovOfficeRegion;
                    }
                }

                /// <summary>
                /// Gets the Government office region details by id.
                /// </summary>
                /// <param name="govOfficeRegionId">The gov office region id.</param>
                /// <returns></returns>
                public GovOfficeRegion GetById(string govOfficeRegionId)
                {
                    var collectParameter = new Collection<SqlParameter>
                                               {
                                                   new SqlParameter("govOfficeRegionId", govOfficeRegionId )
                                               };
                    using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetGovOfficeRegionById,collectParameter))
                    {
                        var result = new GovOfficeRegion();
                        if (reader.Read())
                        {
                            result = ExtractRawData(reader);
                            return result;
                        }
                        return null;
                        

                    }
                }



                /// <summary>
                /// Filters governments by the specified keyword.
                /// </summary>
                /// <param name="keyword">The keyword.</param>
                /// <param name="includeInactive">if set to <c>true</c> [include in active].</param>
                /// <returns></returns>
                public Collection<GovOfficeRegion> Filter(string keyword, bool includeInactive)
                {
                    var listResult = new Collection<GovOfficeRegion>();
                    Collection<GovOfficeRegion> listGov = includeInactive ? GetAll() : GetAllActive();

                    const string digitPattern = "^[0-9]";
                    const string aePattern = "^[a-eA-E]";
                    const string fjPattern = "^[f-jF-J]";
                    const string knPattern = "^[k-nK-N]";
                    const string orPattern = "^[o-rO-R]";
                    const string svPattern = "^[s-vS-V]";
                    const string wzPattern = "^[w-zW-Z]";

                    if(keyword.Equals("ae"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if(checkPattern(aePattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }   
                    }
                    else if (keyword.Equals("fj"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(fjPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("kn"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(knPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("or"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(orPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("sv"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(svPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("wz"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(wzPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("09"))
                    {
                        foreach (var govOfficeRegion in listGov)
                        {
                            if (checkPattern(digitPattern, govOfficeRegion.GovOfficeRegionName))
                            {
                                listResult.Add(govOfficeRegion);
                            }
                        }
                    }
                    else if (keyword.Equals("all"))
                    {
                        return listGov;
                    }
                    else
                    {
                        return null;
                    }
                    return listResult;

                }


        #endregion


                /// <summary>
                /// Checks the pattern.
                /// </summary>
                /// <param name="pattern">The pattern.</param>
                /// <param name="target">The target.</param>
                /// <returns></returns>
                private bool checkPattern(string pattern, string target)
                {
                    var myRegex = new Regex(pattern);
                    var match = myRegex.Match(target);
                    return match.Success;
                }
    }
}
